package com.sinux.generality.basesupport.utils.base;

import com.alibaba.fastjson.JSON;

/**
 * 通用业务层父类
 * @ClassName BaseController
 * @Description: 通用业务层父类
 * @author Tangjc
 * @date 2021年4月26日 下午6:18:00
 */
public class BaseController {
	private static final String RESULT_STRING_BEGIN = "{\"code\":";
	private static final String RESULT_STRING_MID = ",\"data\":";
	private static final String RESULT_STRING_END = ",\"message\":\"\"}";
	private static final String LINK_UP = "\"";

	protected String ok(){
		return ok("", false);
	}

	protected String ok(Object obj){
		return ok(JSON.toJSONString(obj), true);
	}
	
	protected String ok(String json){
		return result("200", json, false);
	}
	
	private String ok(String json, boolean isObj){
		return result("200", json, isObj);
	}
	protected String error(){return error("", false);}

	protected String error(Object obj){
		return error(JSON.toJSONString(obj), true);
	}
	
	protected String error(String json){
		return result("100", json, false);
	}
	
	private String error(String json, boolean isObj){
		return result("100", json, isObj);
	}
	
	private String result(String code, String json, boolean isObj){
		if(isObj){
			return RESULT_STRING_BEGIN+code+RESULT_STRING_MID+json+RESULT_STRING_END;
		}else{
			return RESULT_STRING_BEGIN+code+RESULT_STRING_MID+LINK_UP+json+LINK_UP+RESULT_STRING_END;
		}
	}
	
	
	//测速用
	public static void main(String[] args) {
		final String resultStringBegin = "{\"code\":";
		final String resultStringMid = ",\"message\":\"";
		final String resultStringEnd = "\"}";
		final String linkup = "\"";
		String code = "123";
		String json = "123";
		long beginTime = System.currentTimeMillis();
		System.out.println("开始时间为"+beginTime);
		for(int i=0;i<1000000;i++){
			JSON.toJSONString(json);
			StringBuilder sb = new StringBuilder();
			sb.append(resultStringBegin).append(code).append(resultStringMid).append(json).append(resultStringEnd);
		}
		long midTime = System.currentTimeMillis();
		System.out.println("当前时间为"+midTime);
		System.out.println("差值为"+(midTime-beginTime));
		
//		long beginTime = System.currentTimeMillis();
//		System.out.println("开始时间为"+beginTime);
//		for(int i=0;i<1000000;i++){
//			resultStringBegin.concat(code).concat(resultStringMid).concat(json).concat(resultStringEnd);
//		}
//		long midTime = System.currentTimeMillis();
//		System.out.println("当前时间为"+midTime);
//		System.out.println("差值为"+(midTime-beginTime));
		
		
//		for(int i=0;i<10000;i++){
//			JSONObject json1 = new JSONObject();
//			json1.put("code", code);
//			json1.put("message", json);
//			json1.toJSONString();
//		}
//		long endTime = System.currentTimeMillis();
//		System.out.println("当前时间为"+endTime);
//		System.out.println("差值为"+(endTime-midTime));
		
		boolean a = true;
		for(int i=0;i<1000000;i++){
			JSON.toJSONString(json);
			if(a){
				String s = resultStringBegin+code+resultStringMid+linkup+json+linkup+resultStringEnd;
			}else{
				String s = resultStringBegin+code+resultStringMid+json+resultStringEnd;
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println("当前时间为"+endTime);
		System.out.println("差值为"+(endTime-midTime));
	}
}
